package org.opnlp.opnlp2.io;

import org.opnlp.opnlp2.parser.HTMLCleanerEngine;
import org.opnlp.opnlp2.model.Document;
import org.opnlp.opnlp2.model.Collection;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * IO class implemented to read csv files containing MP's speeches.
 * @author Ilija, Stefan
 * @version 1.1
 */

public class Reader {

    /**
     * Reads data from csv file, and returns whole String
     * @param filePath to csv document
     * @return String containing all data
     * @see model.Document
     * @see model.Collection
     * @author Ilija, Stefan
     * @version 1.1
     */
    public static String readCsv (String filePath){
        String ceoTekst = "";		
        Collection.instance();
	try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String linija = reader.readLine();
            while (linija != null){
                ceoTekst = ceoTekst + linija;
		linija = reader.readLine();
            }
		reader.close();
	} catch (IOException e) {
            e.printStackTrace();
        } 
        return ceoTekst;
    }

    /**
     * Splits text to data units and creates Documents and places them in Collection.
     * @param text containing all data
     * @version 1.0
     * @author Ilija
     */
    public static void parseDocuments (String text){
        String [] data = text.split("#####");
        for (int i = 0; i<data.length; i+=4){
            Document dokument = new Document(Integer.parseInt(data[i]), HTMLCleanerEngine.clean(data[i+1]), HTMLCleanerEngine.clean(data[i+2]), HTMLCleanerEngine.clean(data[i+3]));
            Collection.instance.collectionOfDocuments.add(dokument);           
        }          
    }

}
